package com.kangdi.server.controller;

import com.kangdi.server.pojo.Admin;
import com.kangdi.server.pojo.RespBean;
import com.kangdi.server.service.IAdminService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Map;
import java.util.UUID;

@RestController
public class AdminInfoController {
    @Autowired
    private IAdminService adminService;

    @ApiOperation(value = "更新管理员信息")
    @PutMapping("/admin/info")
    public RespBean updateAdminInfo(@RequestBody Admin admin, Authentication authentication){
        if (adminService.updateById(admin)){
            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(admin, null, authentication.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authentication);
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }
    @ApiOperation(value = "更新密码")
    @PutMapping("/admin/pass")
    public  RespBean updateAdminPassword(Map<String,Object> info){
        String oldPass = (String) info.get("oldPass");
        String  pass = (String) info.get("pass");
        Integer adminId = (Integer) info.get("adminId");
        return adminService.updateAdminPassword(oldPass,pass,adminId);
    }

    @ApiOperation(value = "更新头像")
    @ApiImplicitParams(@ApiImplicitParam(name = "file",value = "头像",dataType = "MultipartFile"))
    @PutMapping("/admin/userface")
    public RespBean updateUserFace(MultipartFile file,Authentication authentication){
        Admin admin= (Admin) authentication.getPrincipal();
        String filename = file.getOriginalFilename();
        int index = filename.lastIndexOf(".");
        String sufixName = UUID.randomUUID().toString();
        String fileName="D:/springboot/yeb/"+sufixName+filename.substring(index);
        InputStream inputStream=null;
        OutputStream outputStream=null;
        try {
            inputStream= file.getInputStream();
            outputStream=new FileOutputStream(new File(fileName));
            byte[] bytes=new byte[1024];
            int length=0;
            while((length=inputStream.read(bytes))>0){
                outputStream.write(bytes,0,length);
            }
            admin.setUserFace(fileName);
            adminService.updateById(admin);
            return RespBean.success("更新成功");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return RespBean.error("更新失败");
    }
}
